{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.0-alpha0\n",
      "sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)\n",
      "matplotlib 3.0.3\n",
      "numpy 1.16.2\n",
      "pandas 0.24.2\n",
      "sklearn 0.20.3\n",
      "tensorflow 2.0.0-alpha0\n",
      "tensorflow.python.keras.api._v2.keras 2.2.4-tf\n"
     ]
    }
   ],
   "source": [
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import sklearn\n",
    "import pandas as pd\n",
    "import os\n",
    "import sys\n",
    "import time\n",
    "import tensorflow as tf\n",
    "\n",
    "from tensorflow import keras\n",
    "\n",
    "print(tf.__version__)\n",
    "print(sys.version_info)\n",
    "for module in mpl, np, pd, sklearn, tf, keras:\n",
    "    print(module.__name__, module.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".. _california_housing_dataset:\n",
      "\n",
      "California Housing dataset\n",
      "--------------------------\n",
      "\n",
      "**Data Set Characteristics:**\n",
      "\n",
      "    :Number of Instances: 20640\n",
      "\n",
      "    :Number of Attributes: 8 numeric, predictive attributes and the target\n",
      "\n",
      "    :Attribute Information:\n",
      "        - MedInc        median income in block\n",
      "        - HouseAge      median house age in block\n",
      "        - AveRooms      average number of rooms\n",
      "        - AveBedrms     average number of bedrooms\n",
      "        - Population    block population\n",
      "        - AveOccup      average house occupancy\n",
      "        - Latitude      house block latitude\n",
      "        - Longitude     house block longitude\n",
      "\n",
      "    :Missing Attribute Values: None\n",
      "\n",
      "This dataset was obtained from the StatLib repository.\n",
      "http://lib.stat.cmu.edu/datasets/\n",
      "\n",
      "The target variable is the median house value for California districts.\n",
      "\n",
      "This dataset was derived from the 1990 U.S. census, using one row per census\n",
      "block group. A block group is the smallest geographical unit for which the U.S.\n",
      "Census Bureau publishes sample data (a block group typically has a population\n",
      "of 600 to 3,000 people).\n",
      "\n",
      "It can be downloaded/loaded using the\n",
      ":func:`sklearn.datasets.fetch_california_housing` function.\n",
      "\n",
      ".. topic:: References\n",
      "\n",
      "    - Pace, R. Kelley and Ronald Barry, Sparse Spatial Autoregressions,\n",
      "      Statistics and Probability Letters, 33 (1997) 291-297\n",
      "\n",
      "(20640, 8)\n",
      "(20640,)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import fetch_california_housing\n",
    "\n",
    "housing = fetch_california_housing()\n",
    "print(housing.DESCR)\n",
    "print(housing.data.shape)\n",
    "print(housing.target.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(11610, 8) (11610,)\n",
      "(3870, 8) (3870,)\n",
      "(5160, 8) (5160,)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "x_train_all, x_test, y_train_all, y_test = train_test_split(\n",
    "    housing.data, housing.target, random_state = 7)\n",
    "x_train, x_valid, y_train, y_valid = train_test_split(\n",
    "    x_train_all, y_train_all, random_state = 11)\n",
    "print(x_train.shape, y_train.shape)\n",
    "print(x_valid.shape, y_valid.shape)\n",
    "print(x_test.shape, y_test.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "scaler = StandardScaler()\n",
    "x_train_scaled = scaler.fit_transform(x_train)\n",
    "x_valid_scaled = scaler.transform(x_valid)\n",
    "x_test_scaled = scaler.transform(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(9.0, shape=(), dtype=float32)\n",
      "tf.Tensor(5.0, shape=(), dtype=float32)\n",
      "tf.Tensor(5.0, shape=(), dtype=float32)\n",
      "tf.Tensor(4.0, shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "# metric使用\n",
    "\n",
    "metric = keras.metrics.MeanSquaredError()\n",
    "print(metric([5.], [2.]))\n",
    "print(metric([0.], [1.]))\n",
    "print(metric.result())\n",
    "\n",
    "metric.reset_states()\n",
    "metric([1.], [3.])\n",
    "print(metric.result())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 0  train mse: 2.104944\t valid mse:  1.152736027725499\n",
      "Epoch 1  train mse: 0.91872215\t valid mse:  0.8896286160432914e: 0.99164706\n",
      "Epoch 2  train mse: 0.7998\t valid mse:  0.8209366091379866\n",
      "Epoch 3  train mse: 0.7054345\t valid mse:  0.7788972844132297se: 0.7028327\n",
      "Epoch 4  train mse: 0.6893323\t valid mse:  0.7392967041328761\n",
      "Epoch 5  train mse: 0.6838636\t valid mse:  0.7068141334218119\n",
      "Epoch 6  train mse: 0.6288853\t valid mse:  0.676981339116993\n",
      "Epoch 7  train mse: 0.6157134\t valid mse:  0.6511911645713215\n",
      "Epoch 8  train mse: 0.5851619\t valid mse:  0.6293785540415384\n",
      "Epoch 9  train mse: 0.5706613\t valid mse:  0.6118971719218474\n",
      "Epoch 10  train mse: 0.5402706\t valid mse:  0.5952621731378016\n",
      "Epoch 11  train mse: 0.54052293\t valid mse:  0.5809133897694022\n",
      "Epoch 12  train mse: 0.53637516\t valid mse:  0.5675094480108381\n",
      "Epoch 13  train mse: 0.51985973\t valid mse:  0.5559689667789274\n",
      "Epoch 14  train mse: 0.515164\t valid mse:  0.5467192232581582se: 0.5145796\n",
      "Epoch 15  train mse: 0.479978\t valid mse:  0.5379682506321743\n",
      "Epoch 16  train mse: 0.50999725\t valid mse:  0.526679171092507\n",
      "Epoch 17  train mse: 0.46633324\t valid mse:  0.5203992950389394\n",
      "Epoch 18  train mse: 0.477215\t valid mse:  0.5148747847712059\n",
      "Epoch 19  train mse: 0.46417397\t valid mse:  0.5108684664543097\n",
      "Epoch 20  train mse: 0.48178193\t valid mse:  0.5032346237395398\n",
      "Epoch 21  train mse: 0.46185252\t valid mse:  0.4973093740644174\n",
      "Epoch 22  train mse: 0.46274024\t valid mse:  0.49307524893381793\n",
      "Epoch 23  train mse: 0.46644682\t valid mse:  0.49080667365353564\n",
      "Epoch 24  train mse: 0.4432642\t valid mse:  0.48643213564593407\n",
      "Epoch 25  train mse: 0.45090625\t valid mse:  0.4818171441196208\n",
      "Epoch 26  train mse: 0.4488629\t valid mse:  0.48045542258747753\n",
      "Epoch 27  train mse: 0.44181794\t valid mse:  0.47517768752912226\n",
      "Epoch 28  train mse: 0.45161572\t valid mse:  0.47219569787517995\n",
      "Epoch 29  train mse: 0.46572572\t valid mse:  0.47206957035969616\n",
      "Epoch 30  train mse: 0.43808585\t valid mse:  0.466308046997983\n",
      "Epoch 31  train mse: 0.43751845\t valid mse:  0.4648199179876645\n",
      "Epoch 32  train mse: 0.41373685\t valid mse:  0.4624802952284964\n",
      "Epoch 33  train mse: 0.42936268\t valid mse:  0.4591652727317869\n",
      "Epoch 34  train mse: 0.43549538\t valid mse:  0.4564868130596492\n",
      "Epoch 35  train mse: 0.41336903\t valid mse:  0.45607885896796246\n",
      "Epoch 36  train mse: 0.43046692\t valid mse:  0.4519846246128156\n",
      "Epoch 37  train mse: 0.42912322\t valid mse:  0.4499456875558165\n",
      "Epoch 38  train mse: 0.4072349\t valid mse:  0.44929765676556316\n",
      "Epoch 39  train mse: 0.41295713\t valid mse:  0.4469238782653186\n",
      "Epoch 40  train mse: 0.40867978\t valid mse:  0.44669546120138526\n",
      "Epoch 41  train mse: 0.40263054\t valid mse:  0.44395406849183744\n",
      "Epoch 42  train mse: 0.3994772\t valid mse:  0.44228469782954477\n",
      "Epoch 43  train mse: 0.40704134\t valid mse:  0.4399421895359583\n",
      "Epoch 44  train mse: 0.40135807\t valid mse:  0.4388833924376266\n",
      "Epoch 45  train mse: 0.41037697\t valid mse:  0.43674971655216643\n",
      "Epoch 46  train mse: 0.41439566\t valid mse:  0.4355941281269153\n",
      "Epoch 47  train mse: 0.40962568\t valid mse:  0.43441572506192067\n",
      "Epoch 48  train mse: 0.39851716\t valid mse:  0.4328888719464885\n",
      "Epoch 49  train mse: 0.40331197\t valid mse:  0.43122653409416056.39889443\n",
      "Epoch 50  train mse: 0.42073172\t valid mse:  0.4294852504029208\n",
      "Epoch 51  train mse: 0.40786812\t valid mse:  0.4287425712693342\n",
      "Epoch 52  train mse: 0.41433766\t valid mse:  0.42699564555562336\n",
      "Epoch 53  train mse: 0.41345644\t valid mse:  0.42702381501432485\n",
      "Epoch 54  train mse: 0.41383186\t valid mse:  0.4242592538388384\n",
      "Epoch 55  train mse: 0.40154928\t valid mse:  0.4231782697883574\n",
      "Epoch 56  train mse: 0.3922401\t valid mse:  0.4234387693958411\n",
      "Epoch 57  train mse: 0.40719342\t valid mse:  0.42240796143768206\n",
      "Epoch 58  train mse: 0.3852795\t valid mse:  0.42158149302297054\n",
      "Epoch 59  train mse: 0.4099938\t valid mse:  0.4208094741191737\n",
      "Epoch 60  train mse: 0.38776717\t valid mse:  0.4194609600149003\n",
      "Epoch 61  train mse: 0.41638777\t valid mse:  0.41710019076108146\n",
      "Epoch 62  train mse: 0.39179453\t valid mse:  0.4178874808168511\n",
      "Epoch 63  train mse: 0.39036337\t valid mse:  0.4163798249391438\n",
      "Epoch 64  train mse: 0.39790294\t valid mse:  0.41494463710357116\n",
      "Epoch 65  train mse: 0.38758674\t valid mse:  0.41396833987338855\n",
      "Epoch 66  train mse: 0.41155463\t valid mse:  0.41337588215185694\n",
      "Epoch 67  train mse: 0.38826972\t valid mse:  0.4119882916304662\n",
      "Epoch 68  train mse: 0.38930225\t valid mse:  0.4114479417128327\n",
      "Epoch 69  train mse: 0.38810772\t valid mse:  0.411542709680951\n",
      "Epoch 70  train mse: 0.38471222\t valid mse:  0.41061533896534735\n",
      "Epoch 71  train mse: 0.39543507\t valid mse:  0.40890688181233065\n",
      "Epoch 72  train mse: 0.37729052\t valid mse:  0.40842533312435475\n",
      "Epoch 73  train mse: 0.38770664\t valid mse:  0.4077723521963252\n",
      "Epoch 74  train mse: 0.3808367\t valid mse:  0.40831397397351854\n",
      "Epoch 75  train mse: 0.37124214\t valid mse:  0.4079214262858768\n",
      "Epoch 76  train mse: 0.38131458\t valid mse:  0.40689089063270617\n",
      "Epoch 77  train mse: 0.39142957\t valid mse:  0.40461315441581913\n",
      "Epoch 78  train mse: 0.3847341\t valid mse:  0.40419731915176016\n",
      "Epoch 79  train mse: 0.3856133\t valid mse:  0.40517933238266035\n",
      "Epoch 80  train mse: 0.3826249\t valid mse:  0.4036865978866542\n",
      "Epoch 81  train mse: 0.38086516\t valid mse:  0.4023046808844443\n",
      "Epoch 82  train mse: 0.37560025\t valid mse:  0.402186101305847\n",
      "Epoch 83  train mse: 0.38003284\t valid mse:  0.40099077221170687\n",
      "Epoch 84  train mse: 0.39618662\t valid mse:  0.400377808022975\n",
      "Epoch 85  train mse: 0.37961963\t valid mse:  0.400851183088995\n",
      "Epoch 86  train mse: 0.38147828\t valid mse:  0.39947008166537423\n",
      "Epoch 87  train mse: 0.39104906\t valid mse:  0.4000694746090793\n",
      "Epoch 88  train mse: 0.39817396\t valid mse:  0.3983560495787654\n",
      "Epoch 89  train mse: 0.39617527\t valid mse:  0.39896695078765876\n",
      "Epoch 90  train mse: 0.38800916\t valid mse:  0.398446036644316\n",
      "Epoch 91  train mse: 0.38297555\t valid mse:  0.39777756211776305\n",
      "Epoch 92  train mse: 0.3598824\t valid mse:  0.3979537550166713\n",
      "Epoch 93  train mse: 0.36405742\t valid mse:  0.39890728342281334\n",
      "Epoch 94  train mse: 0.36883098\t valid mse:  0.3985041265349875\n",
      "Epoch 95  train mse: 0.38816872\t valid mse:  0.39594222937687445\n",
      "Epoch 96  train mse: 0.3903994\t valid mse:  0.3949346546069753\n",
      "Epoch 97  train mse: 0.36976993\t valid mse:  0.39451668831237774\n",
      "Epoch 98  train mse: 0.3692444\t valid mse:  0.39590326441132334se: 0.36070287\n",
      "Epoch 99  train mse: 0.36694872\t valid mse:  0.3949503509951996\n"
     ]
    }
   ],
   "source": [
    "# 1. batch 遍历训练集 metric\n",
    "#    1.1 自动求导\n",
    "# 2. epoch结束 验证集 metric\n",
    "\n",
    "epochs = 100\n",
    "batch_size = 32\n",
    "steps_per_epoch = len(x_train_scaled) // batch_size\n",
    "optimizer = keras.optimizers.SGD()\n",
    "metric = keras.metrics.MeanSquaredError()\n",
    "\n",
    "def random_batch(x, y, batch_size=32):\n",
    "    idx = np.random.randint(0, len(x), size=batch_size)\n",
    "    return x[idx], y[idx]\n",
    "\n",
    "model = keras.models.Sequential([\n",
    "    keras.layers.Dense(30, activation='relu',\n",
    "                       input_shape=x_train.shape[1:]),\n",
    "    keras.layers.Dense(1),\n",
    "])\n",
    "\n",
    "for epoch in range(epochs):\n",
    "    metric.reset_states()\n",
    "    for step in range(steps_per_epoch):\n",
    "        x_batch, y_batch = random_batch(x_train_scaled, y_train,\n",
    "                                        batch_size)\n",
    "        with tf.GradientTape() as tape:\n",
    "            y_pred = model(x_batch)\n",
    "            y_pred = tf.squeeze(y_pred, 1)\n",
    "            loss = keras.losses.mean_squared_error(y_batch, y_pred)\n",
    "            metric(y_batch, y_pred)\n",
    "        grads = tape.gradient(loss, model.variables)\n",
    "        grads_and_vars = zip(grads, model.variables)\n",
    "        optimizer.apply_gradients(grads_and_vars)\n",
    "        print(\"\\rEpoch\", epoch, \" train mse:\",\n",
    "              metric.result().numpy(), end=\"\")\n",
    "    y_valid_pred = model(x_valid_scaled)\n",
    "    y_valid_pred = tf.squeeze(y_valid_pred, 1)\n",
    "    valid_loss = keras.losses.mean_squared_error(y_valid_pred, y_valid)\n",
    "    print(\"\\t\", \"valid mse: \", valid_loss.numpy())\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
